package com.zxl.yjb.http.observer;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;


import com.zxl.yjb.R;
import com.zxl.yjb.activity.LoginActivity;
import com.zxl.yjb.app.AppManager;
import com.zxl.yjb.app.Const;
import com.zxl.yjb.app.MyApplication;
import com.zxl.yjb.bean.LoginBean;
import com.zxl.yjb.bean.LoginSucessBean;
import com.zxl.yjb.http.customexception.ApiException;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.im.SealAppContext;
import com.zxl.yjb.utils.DisplayUtil;
import com.zxl.yjb.utils.LogUtil;
import com.zxl.yjb.utils.SaveBeanUtil;
import com.zxl.yjb.utils.ToastUtils;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by Administrator on 2017/6/11.
 */

public class ProgressObserver<T> implements Observer<T> {

    private static final String TAG = ProgressObserver.class.getSimpleName();
    private ObserverOnNextListener observerOnNextListener;
    private Context context;
    public Disposable disposable;

    public ProgressObserver(ObserverOnNextListener observerOnNextListener, Context context) {
        this.observerOnNextListener = observerOnNextListener;
        this.context = context;
    }


    @Override
    public void onSubscribe(@NonNull Disposable d) {
        this.disposable = d;
//        DisplayUtil.displayLoading(context);
        showDialog();
    }

    public void showDialog() {
        DisplayUtil.displayLoading(context);
    }

    @Override
    public void onNext(@NonNull T t) {
        DisplayUtil.hideLoading(context);
        LogUtil.e(TAG, "onNext: " + t.toString());
        MyApplication.isRefresh = false;
        if (observerOnNextListener != null)
            observerOnNextListener.onNext(t);
    }


    @Override
    public void onError(@NonNull Throwable e) {
        MyApplication.isRefresh = false;
        LogUtil.e(TAG, e.getMessage());
        if (e instanceof SocketTimeoutException) {
            ToastUtils.show(MyApplication.getContext(), context.getResources().getString(R.string.net_imeout));
        } else if (e instanceof ConnectException) {
            ToastUtils.show(MyApplication.getContext(), context.getResources().getString(R.string.net_isconnnet));
        } else if (e instanceof ApiException) {
            ApiException apiException = (ApiException) e;
            if (apiException.getRequestCode() == 503) {
                ToastUtils.show(MyApplication.getContext(), "您的登录状态已过期或在其他设备登录，请重新登录");
             //退出登陆
                AppManager.getInstance().AppExit(MyApplication.getContext());

            }else if (apiException.getRequestCode() == 0001&&e.getMessage().equals("未登录！")){
                ryTokenLogin();
            }else /*if (((ApiException) e).getRequestCode() == 505)*/ {
                ToastUtils.show(MyApplication.getContext(), e.getMessage());
            }
        } else {
//            ToastUtils.show(context,e.getMessage());
        }
        DisplayUtil.hideLoading(context);
        disposable.dispose();
    }

    @Override
    public void onComplete() {
//        DisplayUtil.hideLoading(context);
        disposable.dispose();
    }


    private void ryTokenLogin() {
        Object bean = SaveBeanUtil.getBean(context, Const.VIPINFO);
        if (bean == null) {
            return;
        }
        LoginSucessBean loginSucessBean = (LoginSucessBean) bean;

        Map<String, String> map = new HashMap<>();
        map.put("uid", loginSucessBean.getUid()+"");
        map.put("rytoken", loginSucessBean.getRytoken());
        DataManager dataManager = new DataManager();
        dataManager.loginWithRtoken(new Observer<LoginBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(LoginBean loginBean) {
                if (loginBean == null){
                    return;
                }
                //保存用户信息
//                SaveBeanUtil.putBean(context, Const.VIPINFO, loginBean.getUser());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        },  map);
      /*  dataManager.loginWithRtoken(new ProgressObserver<LoginBean>(new ObserverOnNextListener<LoginBean>() {
            @Override
            public void onNext(LoginBean loginBean) {
                if (loginBean == null){
                    return;
                }
                //保存用户信息
                SaveBeanUtil.putBean(context, Const.VIPINFO, loginBean.getUser());

            }
        }, context), map);*/
    }
}
